<?php
header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
if(!check_admin()) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = 'activites_admin';
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
	$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	$_SESSION['id'] = $id;
	$_SESSION['cat'] = 'activites';
	$user_id = check_cookie();
	if($action == 'del'){
		if($id){
			$activite = new activite($id);
			echo ($activite->delete()) ? "Activit&eacute; effac&eacute;e" : "D&eacute;sol&eacute;, une erreur est survenue";
		}
	}
	elseif($action == 'add_field'){
		$i = (!empty($_REQUEST['valeur'])) ? $_REQUEST['valeur'] : 1;
		$types = array("bouton" => "integer(1)", "nombre" => "integer(3)","texte" => "varchar(255)");
		echo "<input type = 'text' name = 'champs_$i' value = '' size = '20' /> : ";
		echo "<select name = 'type_$i'>";
		foreach($types as $text => $value){
			echo "<option value = '$value'>$text</option>";
		}
		echo "</select>";
		echo "";
		echo "<a style = 'padding-left: 1em;' href = 'javascript:del_field($i)'><img src = 'images/icon_moins.png' alt = '-' height = '20' width = '20' /></a>";
	}
	elseif($action == 'new'){
		if($id) $activite = new activite($id);
		echo ($id) ? "<h1>Modifier l'activit&eacute;</h1>" : "<h1>Nouvelle activit&eacute;</h1>\n";
		$user_id = ($id) ? $activite->responsable : check_cookie();
		$responsable = new membre($user_id);
		echo "<div style = 'positiion: relative;'>\n";
		echo "<div style = 'position: absolute; top: 50px; right: 0px; z-index: 5;' id = 'iframe'>\n";
		echo "    <iframe src='img_upload.php?div=activite' frameborder='' scrolling='0'></iframe>\n";
		echo "</div>\n";
		echo "<form action = 'activites_admin.php' method = 'post' id = 'myform' name = 'myform'>\n";
		echo leftCol("Nom: ",10);
		$value_nom = ($id) ? $activite->nom : "";
		echo rightCol("<input type = 'text' name = 'nom' size = '40' onkeyup = 'writediv(this.value,\"nom_activite\")' value = \"$value_nom\" />",88);
		$content = "<button type = 'button' name = 'citation' onclick = 'inserttag(\"_\",document.myform.corps);preview(document.myform.corps.value,\"corps_activite\");'>_italique_</button>\n";
		$content .= "<button type = 'button' name = 'citation' onclick = 'inserttag(\"*\",document.myform.corps);preview(document.myform.corps.value,\"corps_activite\");'>*gras*</button>\n";		
		$content .= "<button type = 'button' name = 'couleur' onclick = 'inserttag(\"!!\",document.myform.corps);preview(document.myform.corps.value,\"corps_activite\");'>!!couleur!!</button>\n";
		$content .= "<button type = 'button' name = 'citation' onclick = 'insertcode(\" \\\"titre\\\":http://adresse \",document.myform.corps);preview(document.myform.corps.value,\"corps_activite\");'>lien</button>\n";
		echo leftCol("",10);
		echo rightCol($content,88);
		echo leftCol("Description: ",10);
		$value_description = ($id) ? $activite->description : "";
		echo rightCol("<textarea name = 'corps' cols = '40' rows = '20' onkeyup = 'preview(this.value,\"corps_activite\")'>$value_description</textarea>",88);
		echo leftCol("Date:",10);
		if($id){
			if(ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$activite->date,$regs)){
				$datej = $regs[3];
				$datem = $regs[2];
				$datea = $regs[1];
			}
			else {$datej = ""; $datem = ""; $datea = "";}		
		}
		else {$datej = ""; $datem = ""; $datea = "";}
		$col = "<input type = 'text' size = '2' maxlength = '2' name = 'datej' value = '$datej' />.";
		$col .= "<input type = 'text' size = '2' maxlength = '2' name = 'datem' value = '$datem' />.";
		$col .= "<input type = 'text' size = '4' maxlength = '4' name = 'datea' value = '$datea' />\n";
		echo rightCol($col,88);
		echo leftCol("D&eacute;lai:",10);
		if($id){
			if(ereg("([0-9]{4})-([0-9]{2})-([0-9]{2})",$activite->delai,$regs)){
				$delaij = $regs[3];
				$delaim = $regs[2];
				$delaia = $regs[1];
			}
			else {$delaij = ""; $delaim = ""; $delaia = "";}		
		}
		else {$delaij = ""; $delaim = ""; $delaia = "";}
		$col = "<input type = 'text' size = '2' maxlength = '2' name = 'delaij' value = '$delaij' />.";
		$col .= "<input type = 'text' size = '2' maxlength = '2' name = 'delaim' value = '$delaim' />.";
		$col .= "<input type = 'text' size = '4' maxlength = '4' name = 'delaia' value = '$delaia' />\n";
		echo rightCol($col,88);
		echo leftCol("Responsable",10);

		$echo = "<div style = 'position: relative'>";
		$value =  "$responsable->prenom $responsable->nom";
		$echo .= "<input type = 'text' size = '20' name = 'membre' id = 'membre' value = '$value'/>";
		$echo .= "<span id='indicator_responsable' style='display: none'><img src='images/spinner.gif' alt='recherche...' /></span>";
		$autocomplete = "<div id='autocomplete_choices' class='autocomplete'></div>";
		$echo .= "<input type = 'hidden' name = 'membre_id' id = 'membre_id' value = '$responsable->id' />";
		$echo .= "</div>";
		echo rightCol($echo,88);


		echo leftCol("Membres uniq.",10);
		$checked = ($id) ? (($activite->prive) ? "checked = 'checked'" : "") : "";
		echo rightCol("<input type = 'checkbox' name = 'prive' value = '1' $checked />",88);
		echo leftCol("Publi&eacute;e",10);
		$checked = ($id) ? (($activite->valide) ? "checked = 'checked'" : "") : "";
		echo rightCol("<input type = 'checkbox' name = 'valide' value = '1' $checked />",88);
		echo leftCol("",10);
		echo empty_line_two_col();
		echo "<div id='list'></div>\n";
		echo "<div class = 'activite'>\n";
		echo "<h1 id = 'nom_activite'>$value_nom</h1>\n";
		echo "<div class = 'activite_description preview' id = 'corps_activite'>".(prepare_info($value_description))."</div>\n";
		echo "</div>\n";
		$print = ($id) ? "<input type = 'hidden' name = 'id' value = '$id' />\n" : "";
		if(!$id){
			echo "<h2>Champs du formulaire d'inscription</h2>\n";
			$types = array("bouton" => "integer(1)", "nombre" => "integer(3)","texte" => "varchar(255)");
			echo "<div id = 'champs'>\n";
			$i = 0;
			echo "<p id = 'p_$i'><input type = 'hidden' name = 'champs_$i' value = 'Nom' />Nom : ";
			echo "<input type = 'hidden' name = 'type_$i' value = 'varchar(255)' />texte </p>";
			$i++;
			echo "<p id = 'p_$i'><input type = 'hidden' name = 'champs_$i' value = 'Email' />Email : ";
			echo "<input type = 'hidden' name = 'type_$i' value = 'varchar(255)' />texte </p>";
			echo "<p id = 'p_".($i+1)."' ><a style = 'padding-left: 21em; ' href = 'javascript:add_field(".($i+1).")'><img src = 'images/icon_plus.png' alt = '+' height = '20' width = '20' /></a></p>\n";
		}
		else{
			echo "<h2>Exemple du formulaire d'inscription (non fonctionnel)</h2>\n";
			$activite->formulaire("",1);
		}
		echo "</div>\n";
		echo "<h2>Texte de l'email de confirmation</h2>\n";
		$value_nom = ($id) ? $value_nom : "*ACTIVITE*";
		$value_date = ($id) ? format_date($activite->date) : "*DATE*";
		echo leftCol("texte : ",10);
		$texte = ($id) ? $activite->texte_email : 
		"Bonjour,\n\nCeci est une confirmation automatique de votre inscription au $value_nom du $value_date.\n\nPour toute question, veuillez contacter : \n*RESPONSABLE*";	
		echo rightCol("<textarea cols = '40' rows = '10' name = 'texte_email'>$texte</textarea>",88);
		echo "<br class = 'clear' />\n";
		echo "<p class  ='center'>$print\n";
		echo "<button type = 'button' onclick = 'list_activites()'>annuler</button>&nbsp;\n";
		echo "<input type = 'hidden' name = 'action' value = 'add' />\n";
		echo "<button type = 'submit' name = 'submit' value = 'add'>enregistrer</button></p>";
		echo "</form>\n";
		echo "</div>\n";
		echo $autocomplete;

	}
	elseif($action == 'list'){

		echo "<h1>Les activites</h1>\n";
		$q = "select id from activites order by date desc";
		$r = ExecRequete($q);
		$max = mysql_num_rows($r);
		$i = 0;
		$fields = array("nom","date","responsable","valide","actions");
		foreach($fields as $field){
			$$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
		}
		echo "<p class = 'center'><button type = 'button' onclick = 'new_activite()'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvelle activit&eacute;</button></p>\n";

		$table = new table();
		foreach($fields as $field){
			$$field = new table_field($field);
			$$field->print = $field;
			if($field != 'nom' && $field != 'responsable') $$field->styleclass = 'center';
			$table->fields[] = $$field;
			if($field == 'id') $$field->hidden = 1;
		}
		$valide->filter = 0;	
		$table->sense = "DESC";
		$table->order =  "date";
		$table->styleclass = '';
		$table->filter = 1;

		while($s = LigneSuivante($r)){
			$activite = new activite($s->id);
			$responsable = new membre($activite->responsable);
			$row = new table_row();
			foreach($fields as $field){
				if($field == 'nom'){
					$print = (strlen($activite->$field) > 30) ? substr($activite->$field,0,30)."..." : $activite->$field;
				} 
				elseif($field == 'date') $print = format_date($activite->date);
				elseif($field == 'valide') $print = ($activite->$field) ? "<span class = 'oui'>oui</span>" : "<span class = 'non'>non</span>";
				elseif($field == 'responsable') $print = $responsable->prenom." ".(substr($responsable->nom,0,1)).".";
				elseif($field == 'actions'){
					$print =  "<img src = 'images/icon_edit.png' alt = 'edit' height = '14' width = '14' style = 'cursor:pointer' onclick = 'edit_activite($activite->id)'\"/>";
					$print .=  "&nbsp;<img src = 'images/icon_delete.png' alt = 'del' height = '14' width = '14' style = 'cursor:pointer' onclick = 'if(confirm(\"Confirmer la suppression de l activite\"))del_activite($activite->id)' />";						
				}
				else $print = $activite->$field;
				$row->cols[$field] = $print;
			}
			$table->rows[] = $row; 
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
	}
	else{
		$_SESSION['navigation'] = 'activites_admin';
		entete('VBCLL - Bienvenue','','',1);
		if($action == 'add'){
			$corps = (!empty($_REQUEST['corps'])) ? $_REQUEST['corps'] : "";
			$nom = (!empty($_REQUEST['nom'])) ? $_REQUEST['nom'] : "";
			$responsable = (!empty($_REQUEST['membre_id'])) ? $_REQUEST['membre_id'] : check_cookie();
			$membre = new membre($responsable);
			$valide = (!empty($_REQUEST['valide'])) ? $_REQUEST['valide'] : 0;
			$prive = (!empty($_REQUEST['prive'])) ? $_REQUEST['prive'] : 0;
			$texte_email = (!empty($_REQUEST['texte_email'])) ? $_REQUEST['texte_email'] : 
			"Bonjour,\n\nCeci est une confirmation automatique de votre inscription au $value_nom du $value_date.\n\nPour toute question, veuillez contacter : \n*RESPONSABLE*";
			$new_activite = ($id) ? new activite($id) : new activite('new');
			$new_activite->description = $corps;
			$new_activite->nom = $nom;
			$new_activite->responsable = $membre->id;
			$new_activite->valide = $valide;
			$new_activite->prive = $prive;
			$fields = array("datej","datem","datea","delaij","delaim","delaia");
			foreach($fields as $field){
				$$field = (!empty($_REQUEST[$field])) ? $_REQUEST[$field] : "";
				if(!$$field){
					if(strpos($field,"j")) $$field = date('d');
					elseif(strpos($field,'m')) $$field = date('m');
					else $$field = date('Y');
				}
			}
			$new_activite->date = $datea."-".$datem."-".$datej;
			$new_activite->delai = $delaia."-".$delaim."-".$delaij;
			$in = array("*ACTIVITE*","*DATE*","*RESPONSABLE*");
			$out = array($nom,format_date($new_activite->date),$membre->prenom." ".$membre->nom."\n".$membre->email."\n".$membre->tel."\n".$membre->mobile);
			$new_activite->texte_email = str_replace($in,$out,$texte_email);
			if($id){
				if($new_activite->update()){
					$texte = "mise &agrave; jour r&eacute;ussie";
				}
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";			
			}
			else{
				foreach($_REQUEST as $key => $val){
					if(ereg("champs_([0-9]+)",$key,$regs)){
						$name = $val;
						$type = $_REQUEST['type_'.$regs[1]];
						$new_activite->champs[$name] = $type;
					}
				}
				$result = $new_activite->insert();
				if($result){
					unset($_SESSION['rand']);
					$texte = $result;
				} 
				else $texte = "D&eacute;sol&eacute;, une erreur s'est produite";			
			}
			echo "<script type = 'text/javascript'>message(\"$texte\")</script>";
		}
		echo "<div id = 'action'></div>\n";
		echo "<script type = 'text/javascript'>list_activites(1)</script>\n";
		pied();	
	}
	
}

?>
